﻿using System;
using System.Runtime.InteropServices;

namespace Lightray.Hattrick.ChppLib.EntityTypes
{
    [ComVisible(true)]
    public class HattrickAge
    {
        private const int HATTRICK_DAYS_A_YEAR = 112;
        private TimeSpan _timespan = TimeSpan.Zero;

        public int Years
        {
            get
            {
                return _timespan.Days / HATTRICK_DAYS_A_YEAR; // (Hattrick days in as year)
            }
        }

        public int Days
        {
            get
            {
                return _timespan.Days % HATTRICK_DAYS_A_YEAR;
            }
        }

        public TimeSpan ToTimespan()
        {
            return _timespan;
        }

        public HattrickAge(int years, int days)
        {
            _timespan = TimeSpan.FromDays(years * HATTRICK_DAYS_A_YEAR + days);
        }

        public double YearsAsFraction()
        {
            return ToTimespan().TotalDays / (double)HATTRICK_DAYS_A_YEAR;
        }

        public override string ToString()
        {
            return string.Format("{0} years and {1} days", Years, Days);
        }
    }
}
